//企业信息维护

var adduser = require('./adduser');
var template = require('template');
var layer = require('{pub}/layer');
var checkradio = require('{plus}/checkradio');
var tpl = require('../../views/manage');
var request = require('request');

return function(e, elem){
    var that = this;
    request.get('company/get', {id:that.data.company.id}, {
        '200':function(res){
            var _layer = layer.form({
                template:tpl,
                data:res.data.company,
                title:'企业信息维护',
                width:420,
                button:that.data.company.useStatus > 1 ? [{id:'cancel', text:'关闭'}] : null,
                events:{
                    'click .j-add':function(e, elem){
                        var that = this;
                        adduser.call(that, e, elem, _layer, function(res){
                            var data = {
                                user:res.data
                            }
                            var elem = $(template.render(tpl.user, data)).prependTo(_layer.main.find('.userbox'));
                            that.initCom(elem, _layer)
                        })
                    }
                },
                ajax:{
                    url:'company/update'
                },
                onInit:function(self){
                    this.initCom(self.element, self);
                },
                onSuccess:function(self, res){
                    if(res.status == '200'){
                        self.destroy()
                    }
                },
                initCom:function(elem, self){
                    elem.find(':checkbox').checkradio({
                        switches:{
                            off:' ',
                            on:' '
                        },
                        callback:function(ele){
                            request.put('emp/updateStatus', {id:ele.val(), status:ele.prop('checked') ? 0 : 1}, null)
                        }
                    });
                    self.resize();
                }
            })
        }
    })
}